package com.ibeeking.found.upms.cp.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.cp.rest.service.ICpPermissionService;
import com.ibeeking.found.upms.service.common.bo.CpPermissionBO;
import com.ibeeking.found.upms.service.common.bo.CpUserFavorityBO;
import com.ibeeking.found.upms.service.common.param.UserPermissionParam;
import com.ibeeking.found.upms.service.common.vo.CurrentPermissionVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.data.TreeFastUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName PermissionController控制器
 * @Description
 * @Author ibeeking
 * @Date 2020-12-01 18:15
 **/
@RestController
@RequestMapping("/cp/permission/v1")
public class CpPermissionController {

    @Resource
    private ICpPermissionService permissionServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询有效权限菜单树")
    @GetMapping(value = "/queryEffectiveList")
    public List<CurrentPermissionVO> queryEffectiveList(@LoginUser(isFull = true) CurrentUser currentUser, HttpServletRequest request) {
        List<CurrentPermissionVO> currentPermissionVos = permissionServiceImpl.queryEffectiveList(currentUser, request.getHeader(HeaderNounConstant.JWT_JTI));
        return TreeFastUtils.listToTree(currentPermissionVos, GlobalConstant.PARENT_ID);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询用户常用功能Id列表")
    @GetMapping(value = "/queryUserPermissionFavorityByUserId")
    public List<CpUserFavorityBO> queryUserPermissionFavorityByUserId(@LoginUser(isFull = true) CurrentUser currentUser) {
        Long userId = currentUser.getId();
        return permissionServiceImpl.queryUserPermissionFavorityByUserId(userId);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询用户功能收藏列表")
    @GetMapping(value = "/queryUserPermissionFavorityList")
    public List<CpPermissionBO> queryUserPermissionFavorityList(@LoginUser(isFull = true) CurrentUser currentUser) {
        return permissionServiceImpl.queryUserPermissionFavorityList(currentUser);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "添加快捷到用户功能收藏列表")
    @PostMapping(value = "/updateUserPermissionFavority")
    public Boolean updateUserPermissionFavority(@LoginUser(isFull = true) CurrentUser currentUser, @RequestBody @Validated UserPermissionParam userPermissionParam) {
        return permissionServiceImpl.addAndUpdateUserPermissionFavority(currentUser, userPermissionParam);
    }
}
